const mysql = require('../../mysql')


 async function add_template(ctx) {
  const { necessary, dispensable} = ctx.request.body

  try {
    let msg = ''
    const template = await mysql('template')
                          .where({template_name: necessary.template_name})
                          .select()

    if (template.length) {
      try {
        await mysql('template').where('id', necessary.id).update(necessary)
        await mysql('dispensable_parameter').where('template_id', necessary.id).del()
        for (let i = 0; i<dispensable.length; i++) {
          delete dispensable[i].id
          dispensable[i].template_id = necessary.id
        }

        await mysql('dispensable_parameter').insert(dispensable)
        
        msg = '修改成功'
      } catch (err) {
        msg = err
      }
    } else {
      try {
        const id = await mysql('template')
                       .insert(necessary)
        for (let i = 0; i < dispensable.length; i++) {
          dispensable[i].template_id = id
        }
        await mysql('dispensable_parameter')
              .insert(dispensable)
        msg = '添加成功'
      } catch (err) {
        msg = err
      }
      
    }

    ctx.state.data = {
        msg
    }

  } catch (err) {
    ctx.state = {
      code: -1,
      data: {
        msg: err
      }
    }
  }
}

async function validate_name(ctx) {
  const { template_name, template_id } = ctx.request.body
  const template = await mysql('template')
                        .where({"template_name": template_name})
                        .select()
  
  if (template.length) {
    if (template[0].id === template_id) {
      msg = false
    } else {
      msg = true
    }
  } else {
    msg = false
  }
  ctx.state.data = {
    msg
  }
}


async function search_template(ctx) {
  const { template_name, page, page_size } = ctx.request.body

  let template_list = []
  let total = 0

  if (template_name) {
    total = await mysql('template')
                  .where('template_name', 'like', `%${template_name}%`)
                  .select()
    template_list = await mysql('template')
                          .limit(Number(page_size))
                          .offset((Number(page) - 1) * Number(page_size))
                          .where('template_name', 'like', `%${template_name}%`)
                          .orderBy('id', 'desc')
                          .select()
    
  } else {
    total = await mysql('template').select()
    template_list = await mysql('template')
                          .limit(Number(page_size))
                          .offset((Number(page) - 1) * Number(page_size))
                          .orderBy('id', 'desc')
                          .select()
  }
                     
  ctx.state.data = {
    template_list,
    total: total.length
  }
}

async function get_template(ctx) {
  const { template_id } = ctx.request.body

  const essential = await mysql('template')
                          .where({'id': template_id})
                          .select()
  const dispensable = await mysql('dispensable_parameter')
                            .where({template_id})
                            .select()
                          
  ctx.state.data = {
    essential,
    dispensable
  }
}

module.exports = {
  add_template,
  validate_name,
  search_template,
  get_template
}